const wx2my = require('../../../wx2my');
const Behavior = require('../../../Behavior');
var app = getApp();

var base = require('./../../../config.js');

Page({
  data: {
    item: ["全部", "未使用", "已过期", "已使用"],
    seletive_index: 0,
    index: 0,
    page: 1,
    unreceive: [],
    ticket_list: [],
    size: 10,
    noData: false
  },
  onLoad: function (e) {
    console.log(e);
    var o = this,
        t = e.hotel_id;
    o.setData({
      hotel_id: t,
      price: e.money
    }), o.setData({
      color: wx2my.getStorageSync("platform").color
    }), console.log(o.data), wx2my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
      animation: {
        duration: 0,
        timingFunc: "easeIn"
      }
    });
  },
  detail: function (e) {
    var e = e.currentTarget.dataset;
    wx2my.navigateTo({
      url: "../coupon/ticket_detail?ticket_no=" + e.ticketNo + "&time=" + e.time + "&state=" + e.state + "&price=" + e.price + "&name=" + e.name + '&ticket_id=' + e.ticketId + "&ticket_num=" + e.ticketNum
    });
  },
  reload: function (e) {
    var i = this,
        l = app.today(),
        o = wx2my.getStorageSync("userInfo").id,
        s = i.data.index,
        r = i.data.unreceive,
        c = i.data.page,
        size = i.data.size;
    wx2my.request({
      url: base.siteroot + "?i=" + base.acid + "&c=entry&a=wxapp&do=myticket&m=zh_jdgjb&user_id=" + o + "&page=" + c + "&size=" + size + "&state=" + s,
      header: {
        'content-type': 'application/json' // 默认值

      },
      success: function (res) {
        console.log('请求到了');
        console.log(res);

        if (res.data.data) {
          console.log(res);
          var datas = i.data.ticket_list;

          if (res.data.data.is_and) {
            i.setData({
              'noData': true
            });
          }

          ;
          res.data.data.list.forEach(function (item) {
            datas.push(item);
          });
          c++;
          i.setData({
            'ticket_list': datas,
            'page': c
          });
        }
      }
    });
  },
  receive: function (e) {
    var o = e.currentTarget.id;
    console.log(e), console.log(o), 0 == o ? wx2my.navigateTo({
      url: "../hotel_list/hotel_list"
    }) : wx2my.navigateTo({
      url: "../hotel_list/hotel_info?hotel_id=" + o
    });
  },
  not_user: function (e) {
    var o = getCurrentPages();
    o[o.length - 1];
    o[o.length - 2].setData({
      coupon: 0,
      coupons_id: ""
    }), wx2my.navigateBack({
      delta: 1
    });
  },
  seletive_index: function (e) {
    var o = e.currentTarget.dataset.index;
    this.setData({
      seletive_index: o,
      index: o,
      page: 1,
      unreceive: [],
      ticket_list: [],
      'noData': false
    }), this.reload();
  },
  receive_coupon: function (e) {
    console.log(e);
    var o = this.data.price,
        t = e.currentTarget.dataset.money,
        n = e.currentTarget.id,
        a = e.currentTarget.dataset.condition;
    if ("" == a) var i = 0;else i = a.replace(/[^0-9]/gi, "");
    if (console.log("接收到的订单金额为 " + o), console.log("该优惠券的优惠金额为 " + t), console.log("该优惠券的优惠条件为 " + a), console.log("该优惠券的优惠条件为 " + i), console.log("该优惠券的id为 " + n), Number(o) < Number(i)) wx2my.showToast({
      title: "不到使用条件"
    });else if (null != o || null != i) {
      var l = getCurrentPages();
      l[l.length - 1];
      l[l.length - 2].setData({
        coupon: t,
        coupons_id: n,
        condition: i
      }), wx2my.navigateBack({
        delta: 1
      });
    }
  },
  onReady: function () {},
  onShow: function () {
    this.setData({
      seletive_index: 0,
      index: 0,
      page: 1,
      unreceive: [],
      ticket_list: []
    }), this.reload();
  },
  onHide: function () {},
  onUnload: function () {},
  // onPullDownRefresh: function() {
  //   this.setData({
  //     seletive_index: 0,
  //     index: 0,
  //     page: 1,
  //     unreceive: []
  //   }), this.reload(), wx.stopPullDownRefresh();
  // },
  onReachBottom: function () {
    this.reload();
  }
});